import router from './router'
import store from './store'

router.beforeEach(async(to, from, next) => {
  const token = store.state.user.token
  const nextTo = ['/login', '/404']
  if (token) {
  // 如果有token,去跳转到login页面,就跳到主页
    if (to.path === '/login') return next('/')
    // 然后随便跳
    await store.dispatch('user/getUserInfo')
    next()
  } else {
    // 如果没有token，如果路径在nextTo里，这两个随便跳，其他页面就跳到login页面
    if (nextTo.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
  next()
})
